package cn.com.gridinfo.sooc.core.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.login.adapter.NodeServerAdapter;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.core.login.dao.RegsiterDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 选择学校界面
 *
 * @author yingchunli
 */
public class SelectSchoolActivity extends BaseSoocActivity {

    NodeServerAdapter adapter;//适配器
    List<NodeServerInfo> list;//学校名称集合
    RegsiterDao dao;
    @InjectView(R.id.search)
    ClearEditText search;//搜索的内容
    @InjectView(R.id.schoolList)
    ListView schoolList;//存放学校信息的列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_schoollist);
        ButterKnife.inject(this);
        dao = new RegsiterDao(this);
        //数据初始化
        list = new ArrayList<>();
        adapter = new NodeServerAdapter(this, list);
        schoolList.setAdapter(adapter);
        //获取数据
        load();

        //按键完成监听
        search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    onSearchClick();
                }
                return false;
            }
        });
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "选择学校";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //ToolBar的back键
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    /**
     * 监听搜索内容的有无，
     * 搜索内容为空时，获取全部学校数据，不为空时，获取搜索的结果
     */
    @OnTextChanged(value = {R.id.search},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (search.getText().toString().trim().equals("")) {
            //搜索内容为空时
            dao.getSchoolList(null, CodeConst.GET_SCHOOLLIST);
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.GET_SCHOOLLIST:
                //获取学校列表数据
                list.clear();
                list.addAll(dao.getSchoolInfoList());
                Log.d("list的信息：" + list.toString());
                adapter.notifyDataSetChanged();
                break;
        }
    }

    /**
     * 获取数据
     */
    private void load() {
        dao.getSchoolList(null, CodeConst.GET_SCHOOLLIST);
    }

    //ListView的单项点击
    @OnItemClick(R.id.schoolList)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //显示点击的此项的内容
        Log.d("list的单项点击" + list.get(position).toString());
        //跳转到注册页面
        Intent intent = new Intent(this, RegisterActivity.class);
        intent.putExtra("select","1");
        RegsiterDao dao = new RegsiterDao(this);
        dao.setServerName(list.get(position).getSqmc());
        dao.setServerUrl(list.get(position).getXtdz());
        Log.d("serverName->" + dao.getServerName());
        Log.d("serverUrl->" + dao.getServerUrl());
        startActivity(intent);
        AnimUtil.intentSlidIn(this);
        noAnimfinish();
    }

    /**
     * 搜索图标点击事件
     */
    @OnClick(R.id.searchImg)
    void onSearchClick() {
        //去搜索
        String sqmc = search.getText().toString().trim();//搜索内容
        Log.d("search_content->" + sqmc);
        dao.getSchoolList(sqmc, CodeConst.GET_SCHOOLLIST);
        //隐藏软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
